package com.sandking.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.sandking.db.cache.YhCache;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.io.SK_OutputStream;
import java.util.List;
import com.sandking.io.SK_InputStream;
import java.io.ByteArrayInputStream;
import com.sandking.db.cache.YhmrxxCache;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;

/**
 * 用户每日信息
 */
public class Yhmrxx {

	public static final String TABLENAME = "用户每日信息";
	public static final String CLASSNAME = "Yhmrxx"; 
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/** id */
	private int id;
	
	/** 用户_id */
	private int yh_id;
	
	/** 修炼次数 */
	private long xlcs;
	
	
	public Yhmrxx() {
		super();
	}
	
	public Yhmrxx(int id, int yh_id, long xlcs) {
		super();
		this.id = id;
		this.yh_id = yh_id;
		this.xlcs = xlcs;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
		addUpdateColumn("id",id);
	}
	
	public void changeIdWith(int id){
		this.id += id;
		setId(this.id);
	}
	
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}	
	public int getYh_id() {
		return yh_id;
	}
	
	public void setYh_id(int yh_id) {
		this.yh_id = yh_id;
		addUpdateColumn("用户_id",yh_id);
	}
	
	public void changeYh_idWith(int yh_id){
		this.yh_id += yh_id;
		setYh_id(this.yh_id);
	}
	
	public void changeYh_idWithMin(int yh_id,int min){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id < min ? min : this.yh_id;
		setYh_id(this.yh_id);
	}
	
	public void changeYh_idWithMax(int yh_id,int max){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id > max ? max : this.yh_id;
		setYh_id(this.yh_id);
	}
	
	public void changeYh_idWithMaxMin(int yh_id,int max,int min){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id < min ? min : this.yh_id;
		this.yh_id = this.yh_id > max ? max : this.yh_id;
		setYh_id(this.yh_id);
	}	
	public long getXlcs() {
		return xlcs;
	}
	
	public void setXlcs(long xlcs) {
		this.xlcs = xlcs;
		addUpdateColumn("修炼次数",xlcs);
	}
	
	public void changeXlcsWith(long xlcs){
		this.xlcs += xlcs;
		setXlcs(this.xlcs);
	}
	
	public void changeXlcsWithMin(long xlcs,long min){
		this.xlcs += xlcs;
		this.xlcs = this.xlcs < min ? min : this.xlcs;
		setXlcs(this.xlcs);
	}
	
	public void changeXlcsWithMax(long xlcs,long max){
		this.xlcs += xlcs;
		this.xlcs = this.xlcs > max ? max : this.xlcs;
		setXlcs(this.xlcs);
	}
	
	public void changeXlcsWithMaxMin(long xlcs,long max,long min){
		this.xlcs += xlcs;
		this.xlcs = this.xlcs < min ? min : this.xlcs;
		this.xlcs = this.xlcs > max ? max : this.xlcs;
		setXlcs(this.xlcs);
	}	
	
	//用户_id
	public Yh getYhPkYh_id(){
		return YhCache.getById(yh_id);
	}
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("yh_id", this.yh_id);
        result.put("xlcs", this.xlcs);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("用户_id", this.yh_id);
        result.put("修炼次数", this.xlcs);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
		    SK_OutputStream.writeInt(out,this.id);
		    SK_OutputStream.writeInt(out,this.yh_id);
		    SK_OutputStream.writeLong(out,this.xlcs);
		    return out.toByteArray();
    	}catch (Exception e) {
            throw e;
        }
    }
    
     public static Yhmrxx createForBytes(byte[] _byte) throws Exception {
     	try (ByteArrayInputStream in = new ByteArrayInputStream(_byte);) {
	     	Yhmrxx yhmrxx = new Yhmrxx();
		    yhmrxx.id = SK_InputStream.readInt(in,null);
		    yhmrxx.yh_id = SK_InputStream.readInt(in,null);
		    yhmrxx.xlcs = SK_InputStream.readLong(in,null);
		    return yhmrxx;
    	}catch (Exception e) {
            throw e;
        }
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhmrxx> createForColumnNameList(List<Map<String, Object>> list){
    	List<Yhmrxx> yhmrxxs = new ArrayList<Yhmrxx>();
		for (Map<String, Object> map : list) {
			yhmrxxs.add(createForColumnNameMap(map));
		}
		return yhmrxxs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhmrxx createForColumnNameMap(Map<String, Object> map){
    	Yhmrxx obj = new Yhmrxx();
	    obj.id = SK_Map.getInt("id", map);
	    obj.yh_id = SK_Map.getInt("用户_id", map);
	    obj.xlcs = SK_Map.getLong("修炼次数", map);
        return obj;
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhmrxx> createForList(List<Map<String, Object>> list){
    	List<Yhmrxx> yhmrxxs = new ArrayList<Yhmrxx>();
		for (Map<String, Object> map : list) {
			yhmrxxs.add(createForColumnNameMap(map));
		}
		return yhmrxxs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhmrxx createForMap(Map<String, Object> map){
    	Yhmrxx obj = new Yhmrxx();
	    obj.id = SK_Map.getInt("id", map);
	    obj.yh_id = SK_Map.getInt("yh_id", map);
	    obj.xlcs = SK_Map.getLong("xlcs", map);
        return obj;
    }
    
    public static Yhmrxx createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Yhmrxx> createForJson(List<String> jsons){
    	List<Yhmrxx> yhmrxxs = new ArrayList<Yhmrxx>();
    	for(String json : jsons){
    		yhmrxxs.add(createForJson(json));
    	}
    	return yhmrxxs;
    }
    
    /** 延迟插入数据库 */
    public Yhmrxx insert(){
    	return YhmrxxCache.insert(this);
    }
    /** 延迟更新数据库 */
    public Yhmrxx update(){
    	return YhmrxxCache.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return YhmrxxCache.delete(this);
    }
    /** 即时插入数据库 */
    public Yhmrxx insertAndFlush(){
    	return YhmrxxCache.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Yhmrxx updateAndFlush(){
    	return YhmrxxCache.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return YhmrxxCache.deleteAndFlush(this);
    }
}